package com.pengtu.gsj.service.impl.check.yh.bridgeTunnel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.dl.BridgeBasicInfoDao;
import com.pengtu.gsj.dao.dl.TunnelInfoDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.dl.BridgeBasicInfo;
import com.pengtu.gsj.entity.dl.TunnelInfo;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.yh.bridgeTunnel.BriTunCheckService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class BriTunCheckServiceImpl implements BriTunCheckService {
	
	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public BridgeBasicInfoDao bridgeBasicInfoDao;
	@Autowired
	public TunnelInfoDao tunnelInfoDao;
	
	@Override
	public Map<String, Object> getBriTunChecks(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		BridgeBasicInfo bridgeBasicInfo = null;
		TunnelInfo tunnelInfo = null;
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		String secondString = (String)jsonObject.get("secondString");
		String tunnId = (String)jsonObject.get("itemProjectId");
		double tableFull = yearFull + yearPart;
		double temp = 0;//用来保存桥梁的分数
		double total = 0;
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		map.put("att", list);
		
		//1、若桥梁有检索条件就对检索后的桥梁数据进行算分
		if (StringUtils.isNotEmpty(secondString)) {
			//根据id检索桥梁数据
			bridgeBasicInfo = bridgeBasicInfoDao.get(secondString);
			list = systemManageService.getCheckAttachmentByTag(startTime, endTime, "qsjc_qlbg");
			//如果桥梁记录/报告中包含了对应的桥梁的名字，说明有对应的记录规范
			for (Attachment attachment : list) {
				if (0 <= attachment.getFileName().indexOf(bridgeBasicInfo.getBridgeName())) {
					temp ++;
					map.put("bri", true);
					break;
				};
			}
			temp = temp > 0 ? yearFull : yearFull * 0.75;
		}
		
		//2、若隧道有检索条件就对检索后的隧道数据进行算分
		if (StringUtils.isNotEmpty(tunnId)) {
			//根据id检索隧道数据
			tunnelInfo = tunnelInfoDao.get(tunnId);
			list = systemManageService.getCheckAttachmentByTag(startTime, endTime, "qsjc_qlbg");
			//如果桥梁记录/报告中包含了对应的桥梁的名字，说明有对应的记录规范
			for (Attachment attachment : list) {
				if (0 <= attachment.getFileName().indexOf(tunnelInfo.getTunnelname())) {
					total ++;
					map.put("tun", true);
					break;
				};
			}
			total = total > 0 ? yearPart : yearPart * 0.75;
			map.put("scoreTwo", total);
		}
		//3、计算两个表的总分和完善度
		total += temp;
		//根据总得分算出完善度
		double percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("scoreOne", temp);
		map.put("percent", percentage);
		map.put("briInfo", bridgeBasicInfo);
		map.put("tunInfo", tunnelInfo);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
}
